<?php 

class suipaiController extends appController
{
	
	public function clear()
	{
		unset($_SESSION['wxid']);
	}
	
	public function index()
	{
		
		
// 		$_SESSION['wxid'] = 1;
		$this->check_member();
		
		$config[c] = array('site_name' => "报料-".Model('app')->get_cfg('site_name'));
		$this->assign_global($config);
		
		$jsdk = Model('suipai')->get_jsdk();
		
		$this->assign("jsdk", $jsdk);
		
		$this->display("index.html");
	}
	
	
	
	public function send_set()
	{
		$res = array('error' => 1, 'content' => '');
		$info['wxid'] = $_SESSION['wxid'];
		$is_exist = Model('suipai')->check_is_exist($info['wxid']);
		if($is_exist > 0)
		{
			$res['content'] = "一天只能报料一次,你今天已经报料了。";
			$this->showMsg($res);
		}
		$info['content'] = yf_empty($_POST['content'], "请输入内容");
		$info['phone'] = yf_empty($_POST['phone'], "请输入手机号");
		if(hy_check($info['phone'], 'm') == '#@_error' && hy_check($info['phone'], 't') == '#@_error')
		{
			$res['content'] = "你输入的电话格式不正确";
			$this->showMsg($res);
		}
		$img = yf_empty($_POST['img'], "请上传至少一张图片");
		$arr = array();
		if(!is_array($img))
		{
			$arr[] = $img;
		}
		else
		{
			$arr = $img;
		}
		$info['pic'] = json_encode($arr);
		$info['created'] = yf_time();
		$id = Model('suipai')->insert_info($info);
		if($id)
		{
			$res = array('error' => 0, 'content' => '报料成功');
		}
		else 
		{
			$res['content'] = "系统繁忙";
		}
		$this->showMsg($res);
	}
	
	public function upload_img()
	{
		$res = array('error' => 1, 'content' => '');
		$media_id = yf_empty($_POST['img'], "error");
		$token = Model('suipai')->getAccessToken();
		$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token."&media_id=".$media_id;
		$open=fopen("log.txt","a" );
		fwrite($open,$url."\r\n");
		fclose($open);
		$img = yf_time().".jpg";
		$img = $this->GrabImage($url, $img);
		$temp = str_replace(ROOT_PATH, "/", $img);
		$html = "<input type=\"hidden\" name=\"img\" value=\"$temp\">";
		$url = "<img src=\"$temp\" style=\"width:32%;padding:0.5%\"  class=\"pre-img\"    >";
		$res = array('error' => 0, 'content' => $html , 'url' =>$url ,'img'=>'http://g4.shfyuan.com/ws/'.$temp);
		$this->showMsg($res);
	}
	
	public function GrabImage($url, $filename = "") {
		if($url == ""):return false;endif;
		if($filename == "") {
			$ext = strrchr($url, ".");
			if($ext != ".gif" && $ext != ".jpg"):return false;endif;
			$filename = date("YmdHis").$ext;
		}
		ob_start();
		readfile($url);
		$img = ob_get_contents();
		ob_end_clean();
		$size = strlen($img);
// 		$file = ROOT_PATH.'Uploads/img/'.date('Ym');
		$file = 'Uploads/img/'.date('Ym');
		if(!is_dir($file))
		{
			mkdir($file);
		}
		$file = $file."/".$filename;
		$fp2 = @fopen($file, "a");
		fwrite($fp2, $img);
		fclose($fp2);
		return $file;
	}
	
	
	public function cancel(){
		$config[c] = array('site_name' => "核销-".Model('app')->get_cfg('site_name'));
		$this->assign_global($config);
		$this->display('cancel_code.html');
	}
	
	public function get_code(){
		$code = isset($_POST['codes'])?$_POST['codes']:"";
		if(!$code){
			$res = array("error"=>1,"content"=>"请输入Code值");
			$this->showMsg($res);
		}
		$access_token = Model('app')->getAccessToken();
		$url = "https://api.weixin.qq.com/card/code/get?access_token=".$access_token;
		$info['code'] = $code;
		$info['check_consume'] = false;
		$result = json_decode($this->post_it($url, json_encode($info)),true);
		if($result['errcode']!=0){
			$res = array("error"=>1,"content"=>"Code值有问题，请确认后再次输入");
			$this->showMsg($res);
		}
		if($result['can_consume']==0&&$result['user_card_status']!="NORMAL"){
			$res = array("error"=>1,"content"=>"请确认卡券状态后再次输入");
			$this->showMsg($res);
		}
		$card_name = Model("suipai")->get_card_name($result['card']['card_id']);
		if(!$card_name){
			$res = array('error'=>1,"content"=>"该卡券不属于小王子门票");
			$this->showMsg($res);
		}
		$result['card']['card_name'] = $card_name;
		$res = array('error'=>0,"content"=>"卡券正常");
		$res['codeData'] = $result;
		$this->showMsg($res);
		
	}
	public function cancelCode(){
		$code = isset($_POST['codes'])?$_POST['codes']:"";
		if(!$code){
			$res = array("error"=>1,"content"=>"请输入Code值");
			$this->showMsg($res);
		}
		$access_token = Model('app')->getAccessToken();
		$url = "https://api.weixin.qq.com/card/code/get?access_token=".$access_token;
		$info['code'] = $code;
		$info['check_consume'] = false;
		$result = json_decode($this->post_it($url, json_encode($info)),true);
		if($result['errcode']!=0){
			$res = array("error"=>1,"content"=>"Code值有问题，请确认后再次输入");
			$this->showMsg($res);
		}
		if($result['can_consume']==0&&$result['user_card_status']!="NORMAL"){
			$res = array("error"=>1,"content"=>"请确认卡券状态后再次输入");
			$this->showMsg($res);
		}
		$card_name = Model("suipai")->get_card_name($result['card']['card_id']);
		if(!$card_name){
			$res = array('error'=>1,"content"=>"该卡券不属于小王子门票");
			$this->showMsg($res);
		}
		$result['card']['card_name'] = $card_name;
		$cancel = "https://api.weixin.qq.com/card/code/consume?access_token=".$access_token;
		$post = array("code"=>$code,"card_id"=>$result['card']['card_id']);
		$resu = json_decode($this->post_it($cancel, json_encode($post)),true);
		if($resu['errcode']!=0){
			$res = array("error"=>1,"content"=>"核销卡券失败，请重新输入");
			$this->showMsg($res);
		}else{
			$res = array("error"=>0,"content"=>"核销卡券成功","result"=>$result);
			$this->showMsg($res);
		}
	}
	
	/**
	 * 用post方法获取数据
	 * @param $url
	 * @param $data
	 * @return
	 */
	public function post_it($url, $data)
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$result = curl_exec($curl);
		if($result){
			curl_close($curl);
			return $result;
		}
		else {
			$error = curl_errno($curl);
			curl_close($curl);
			return false;
		}
	}
}

?>